unit Unit20;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, EditBtn,
  StdCtrls, Buttons, ExtCtrls, DBGrids, Unit19, IBQuery, IBDatabase, db, LCLType;

type

  { TForm20 }

  TForm20 = class(TForm)
    BitBtn1: TBitBtn;
    Button1: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    ComboBox1: TComboBox;
    Datasource1: TDatasource;
    DateEdit1: TDateEdit;
    DBGrid1: TDBGrid;
    Edit1: TEdit;
    Edit2: TEdit;
    ibq1: TIBQuery;
    ibq2: TIBQuery;
    ibt1: TIBTransaction;
    ibt2: TIBTransaction;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Memo1: TMemo;
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    Panel4: TPanel;
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Panel3Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
    dok_type       : TDokumentType;
    kontrahent_idx : integer;
    naglowki_idx   : integer;

    function DodajDok:boolean;
    procedure FillComboBox;
  end;

var
  Form20: TForm20;

implementation

{$R *.lfm}

{ TForm20 }

uses
  unit13, // asortyment
  unit22, // okienko ilosci
  unit21; // kontrahenci



// dodanie nowego dokumentu
procedure TForm20.Button3Click(Sender: TObject);
var
  kartoteka_idx: integer;
  ilosc        : Double;
begin
  if naglowki_idx = -1 then if not DodajDok then exit;
  while  form13.ShowModal = mrOK do  // w pętli asortyment
  begin
    kartoteka_idx:=form13.ibq1.FieldByName('kartoteki_idx').AsInteger;

    if form22.ShowModal = mrOK then // pytanie o ilosc
    begin
       ilosc:=StrToFloat(form22.Edit1.Text);

       ibq2.SQL.Clear;
       ibq2.SQL.Add('insert into pozycje (naglowki_idx,kartoteka_idx,ilosc) values ( :a, :b, :c )');
       ibq2.ParamByName('a').AsInteger:=naglowki_idx;
       ibq2.ParamByName('b').AsInteger:=kartoteka_idx;
       ibq2.ParamByName('c').AsFloat  :=ilosc;
       try
         ibt2.StartTransaction;
         ibq2.ExecSQL;
         ibt2.Commit;
       except
         ibt2.Rollback;
         Application.MessageBox('Nie mogę dodac nowej pozycji !!!','Blad',MB_ICONHAND+MB_OK);
       end;
       // odswiezenie zawartosci okienka
       form20.FormShow(nil);
       application.ProcessMessages;

    end;
  end;

  // odswiezenie zawartosci okienka
  form20.FormShow(nil);
  application.ProcessMessages;

end;

procedure TForm20.FormCreate(Sender: TObject);
begin

end;

procedure TForm20.FormShow(Sender: TObject);
begin
  if ibt1.Active then ibt1.Commit;
  ibq1.SQL.Clear;
  ibq1.SQL.Add('select k.nazwadl, k.nazwaskr, p.ilosc from pozycje p, kartoteki k ');
  ibq1.SQL.Add('where k.kartoteki_idx = p.kartoteka_idx and p.naglowki_idx = '+IntToStr(naglowki_idx));
  try
    ibt1.StartTransaction;
    ibq1.Open;
  except
    ibt1.Rollback;
  end;
end;

procedure TForm20.Panel3Click(Sender: TObject);
begin

end;

function TForm20.DodajDok:boolean;
var
  id           : integer;
  sgn          : string;
  numer        : string;

begin
  DodajDok:=false;

  // wyodrebnienie sygnatury
  sgn:=ComboBox1.Text;
  while (length(sgn) > 0) and (sgn[length(sgn)] <> '-') do delete(sgn,length(sgn),1);
  if (sgn <> '') and  (sgn[length(sgn)] = '-') then delete(sgn,length(sgn),1);
  sgn:=trim(sgn);
  if sgn = '' then
  begin
    Application.MessageBox('Prosze wybrac rodzaj dokumentu !','Blad',MB_ICONHAND+MB_OK);
    exit;
  end;

  // blokowanie wybranych kontrolek, majacych znaczrnienie
  ComboBox1.Enabled:=false; // blokowanie typu dokumentu
  Edit2.Enabled:=false;     // blokowanie kontrahenta
  Button1.Enabled:=false;   // blokowanie kontrahenta
  // pokazanie przycikow OK, NIE - okienko asortymentu
  form13.bt_anuluj.Enabled:=true;
  form13.bt_anuluj.Visible:=true;
  form13.bt_ok.Enabled:=true;
  form13.bt_ok.Visible:=true;
  // schowanie przysku KONIEC - okienko asortymentu
  form13.bt_zamknij.Visible:=false;
  form13.bt_zamknij.Enabled:=false;

  GetNextDokNr(sgn, id, numer); // pobranie kolejnego numeru dla dokumentu
  form20.Caption:='Nowy dokument: '+sgn+' '+numer;

  if ibt2.Active then ibt2.Rollback;
  try
    ibt2.StartTransaction;
    ibq2.SQL.Clear;
    ibq2.SQL.Add('insert into naglowki (nrdok,kontrahent_idx,datadok,defdok_idx) values ( :a, :b , :c , :d )');
    ibq2.ParamByName('a').AsString :=sgn+' '+numer;
    ibq2.ParamByName('b').AsInteger:=form20.kontrahent_idx;
    ibq2.ParamByName('c').AsDate   :=DateEdit1.Date;
    ibq2.ParamByName('d').AsInteger:=id;
    ibq2.ExecSQL;
    // pobranie ID dodanego rekordu
    ibq2.SQL.Clear;
    ibq2.SQL.Add('select gen_id(GEN_NAGLOWKI, 0) from rdb$database');
    ibq2.Open;
    naglowki_idx:=ibq2.Fields[0].AsInteger;
    ibt2.Commit;
    form20.Caption:=form20.Caption+' ('+IntToStr(naglowki_idx)+')';
  except
    ibt2.Rollback;
    Application.MessageBox('Nie mogę dodac nowego dokumentu !!!','Blad',MB_ICONHAND+MB_OK);
    exit;
  end;

  DodajDok:=true;
end;

// wybranie kontrahenta
procedure TForm20.Button1Click(Sender: TObject);
begin
  if form21.ShowModal = mrOK then
  begin
    edit2.Text:=form21.ibq1.FieldByName('nazwaskr').AsString;
    kontrahent_idx:=form21.ibq1.FieldByName('kontrahent_idx').AsInteger;
  end;
end;

procedure TForm20.FillComboBox;
begin
  ComboBox1.Clear;
  if ibt2.Active then ibt2.Rollback;
  ibq2.SQL.Clear;
  ibq2.SQL.Add('select * from defdok');
  case dok_type of
    dtRozchod  : ibq2.SQL.Add('where typdokumentu = 1');
    dtPrzychod : ibq2.SQL.Add('where typdokumentu = 2');
  end;
  ibq2.SQL.Add('order by Upper(sygnatura)');
  try
    ibt2.StartTransaction;
    ibq2.Open;

    ibq2.First;
    while not ibq2.EOF do
    begin
      ComboBox1.Items.Add(ibq2.FieldByName('sygnatura').AsString+' - '+ibq2.FieldByName('opis').AsString);
      ibq2.Next;
    end;
    ibt2.Commit;
  except
    ibt2.Rollback;
  end;
end;

end.

